<?php
namespace app\bis\controller;

use app\common\facade\BisLocation;
use app\common\facade\Category;
use app\common\facade\City;
use think\Controller;
use think\Request;
use app\common\facade\Deal as DealFacade;

class Deal extends Base
{
    // TODO:未做
    public function index()
    {
        return $this->fetch();
    }

    public function add(Request $request)
    {
        $bisId = $this->getLoginUser()->bis_id;

        if ($request->isPost())
        {
            $data = $request->post();
            // TODO:数据校验

            $location = model('BisLocation')->get($data['location_ids'][0]);
            $deals = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'image' => $data['image'],
                'category_id' => $data['category_id'],
                'se_category_id' => empty($data['se_category_id']) ? '': implode(',', $data['se_category_id']),
                'city_id' => $data['city_id'],
                'location_ids' => empty($data['location_ids']) ? '':implode(',', $data['location_ids']),
                'start_time' => strtotime($data['start_time']),
                'end_time' => strtotime($data['end_time']),
                'total_count' => $data['total_count'],
                'origin_price' => $data['origin_price'],
                'current_price' => $data['current_price'],
                'coupons_begin_time' => strtotime($data['coupons_begin_time']),
                'coupons_end_time' => strtotime($data['coupons_end_time']),
                'notes' => $data['notes'],
                'description' => $data['description'],
                'bis_account_id' => $this->getLoginUser()->id,
                'xpoint' => $location->xpoint,
                'ypoint' => $location->ypoint,

            ];
            $id = model('Deal')->add($deals);
            if ($id)
            {
                $this->success('添加成功', 'deal/index');
            }
            else
            {
                $this->error('添加失败');
            }
        }
        else
        {
        // 获取一级城市数据
        $citys = model('City')->getNormalCitysByparentId();

        // 获取一级栏目数据
        $categorys = model('Category')->getNormalCategorysByparentId();

//        dump(model('BisLocation')->getNormalLocationByBisId($bisId));
//        exit();

        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
            'bislocations' => model('BisLocation')->getNormalLocationByBisId($bisId)
        ]);
        return $this->fetch();
        }
    }
}
